#pragma once

#include <QObject>
#include <QObject> 
#include <QTimer>
#include <QTcpSocket>
#include <QMutex>
#include <QObject>
#include <QThread>

#include "commonDefine.h"

#include "SerialPortClient.h"
#include "TcpClient.h"

namespace CommonComponet
{
	class ComDeviceControl : public QObject
	{
		Q_OBJECT
	public:
		ComDeviceControl(QObject* parent);
		~ComDeviceControl();


	public:
		void reconnect(const QString& com_port, const QString command = "");
		void reconnectIp(const QString& ip, int port);

	public:

	public:
		void AsyncSendmessage();
		void AsyncAndWaitSendmessage();
		void AsyncAndWaitSendmessage(int write_timeout, int read_timeout);

	public:
		void Start(const QString& port, const QString& command, DEVICE_CONN_TYPE device_conn_type, int baudRate);
		void StartIpDevice(const QString& ip, int port, const QString& command, DEVICE_CONN_TYPE device_conn_type);

	private:
		void Stop();

	signals:
		void sig_Reconnect(const QString& comport);
		void sig_ReconnectIp(const QString& ip, int port);
		void sig_AsyncSendmessage(const QString& message);
		void sig_AsyncAndWaitSendmessage(const QString& message, int write_timeout, int read_timeout);

	signals:
		void sig_connection_state(int state);
		void sig_receiveDataFromServer(QString);

	private:
		QThread m_workerThread;
		SerialPortClient* m_serial_worker = NULL;
		TcpClient* m_tcp_worker = NULL;
		bool m_running = false;

	private:
		QString m_command;
	};
}
